package lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class CollectionTraversal {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 2, 3, 1));
        //one
        for (Integer i : list) {
            System.out.print(i);
            System.out.print(" ");
        }
        System.out.println();
        //two
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i));
            System.out.print(" ");
        }
        System.out.println();
        //three 迭代器遍历 遍历中使用remove()：删除当前next()获取的元素,唯一安全删除集合元素的方式
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next());
            System.out.print(" ");
        }
        System.out.println();
        //four
        list.forEach(System.out::print);
    }
}
